package com.zhitongyun.devlop.net;

import android.util.Log;

import com.google.gson.JsonObject;
import com.zhitongyun.devlop.db.bean.AppCategoryEntity;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by huanglei on 5/30/16.
 */
public class HaleyPluginRequest {

    public static final String rootUrl = "http:/172.17.0.182:8888/";

    public static void getAppCategory(String upTime, final RequestCallBack back) {
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("categoryUpTime", upTime == null ? "" : upTime);

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(rootUrl + "HaleyPluginServer/")
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        HaleyPlauginService service = retrofit.create(HaleyPlauginService.class);

        service.getAppCategory(jsonObject).enqueue(new Callback<List<AppCategoryEntity>>() {
            @Override
            public void onResponse(Call<List<AppCategoryEntity>> call, Response<List<AppCategoryEntity>> response) {
                if (response.isSuccessful()) {
                    List<AppCategoryEntity> appCategoryEntities = response.body();
                    if (back != null)
                        back.back(true, appCategoryEntities);
                } else {
                    if (back != null)
                        back.back(false, null);
                }
            }

            @Override
            public void onFailure(Call<List<AppCategoryEntity>> call, Throwable t) {
                Log.i("aa", "onFailure "+ t.getMessage());
                if (back != null)
                    back.back(false, null);
            }
        });
    }

}
